<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 菜单管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="名称" class="filter-item handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" class="filter-item" @click="handleAdd">新增</el-button>
            </div>
            <el-table
                    v-loading="loading"
                    :data="tableData"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    :default-expand-all="defaultExpandAll"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column prop="name" label="名称" width="180"></el-table-column>
                <el-table-column prop="icon" label="图标" ></el-table-column>
                <el-table-column prop="perms" label="权限" ></el-table-column>
                <el-table-column prop="orderNum" label="排序"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseDateTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-plus"
                                @click="handleMenuAdd(scope.$index, scope.row)"
                        >新增</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="上级菜单" v-show="form.showBoolean">
                    <select-tree v-model="form.selected" :options="tableData" :props="defaultProps" />
                </el-form-item>
                <el-form-item label="上级菜单" disabled="false" v-show="!form.showBoolean">
                    <el-input v-model="form.parentId" v-show="false"></el-input>
                    <el-input v-model="form.parentName"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-radio-group v-model="form.type">
                        <el-radio label="0">目录</el-radio>
                        <el-radio label="1">菜单</el-radio>
                        <el-radio label="2">按钮</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item v-if="form.type == '0'" label="图标" prop="path">
                    <el-input v-model="form.icon" placeholder="请输入图标" />
                </el-form-item>
                <el-form-item v-if="form.type == '1'" label="路由地址" prop="path">
                    <el-input v-model="form.url" placeholder="请输入路由地址" />
                </el-form-item>
                <el-form-item v-if="form.type != '0'" label="权限标识">
                    <el-input v-model="form.perms" placeholder="请权限标识" maxlength="50" />
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="form.orderNum"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { sysMenuList,saveSysMenu,updateSysMenu,delSysMenu } from '../../api/sysMenu';

import SelectTree from '../../components/common/SelectTree.vue';

export default {
    name: 'sys-menu-list',
    data() {
        return {
            // 遮罩层
            loading: true,
            query: {
                userName: ''
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            defaultExpandAll:false,
            editVisible: false,
            form: {
                id: "",
                name: "",
                type:'0',
                url:"",
                perms:"",
                icon:"",
                orderNum:0,
                // 默认选中值
                selected: '',
                showBoolean:true
            },
            idx: -1,
            id: -1,
            // 数据默认字段
            defaultProps: {
                parent: 'parentId',   // 父级唯一标识
                value: 'id',          // 唯一标识
                label: 'name',       // 标签显示
                children: 'children', // 子级
            }
        };
    },
    components: {
        SelectTree
    },
    created() {
        this.defaultform = JSON.parse(JSON.stringify(this.form));
        this.getData();
        // 创建
    },
    destroyed() {
        this.defaultform = null;
    },
    methods: {
        // 选择图标
        selected(name) {
            this.form.icon = name;
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            this.loading = true;
            sysMenuList(this.query).then(res => {
                this.loading = false;
                this.tableData = res.data;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    delSysMenu(row.id).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`删除成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                })
                .catch(() => {});
        },
        handleAdd() {
            this.form = Object.assign({}, this.defaultform);
            this.editVisible = true;
            this.form.selected = 0;
            this.form.type = '0';
            this.form.showBoolean = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        handleMenuAdd(index, row){
            this.idx = index;
            this.form = Object.assign({}, this.defaultform);
            this.form.parentId = row.id;
            this.form.parentName = row.name;
            this.form.type = '1';
            this.form.deptShowBoolean = false;
            this.editVisible = true;
            this.form.selected = 0;
            this.form.showBoolean = false;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form.id = row.id;
            this.form.name = row.name;
            this.form.url = row.url;
            this.form.perms = row.perms;
            this.form.parentId = row.parentId;
            this.form.parentName = row.parentName;
            this.form.type = row.type;
            this.form.icon = row.icon;
            this.form.orderNum = row.orderNum;
            this.editVisible = true;
            this.form.selected = row.id
            this.form.showBoolean = false;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        // 保存编辑
        saveEdit() {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    console.log('error submit!!');
                    return false;
                }
                if (this.form.id) {
                    updateSysMenu(this.form).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`修改成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                } else {
                    saveSysMenu(this.form).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`保存成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                }
            });
        }
    },
    filters:{
        showStatus(status){
            if (status == "1") {
                return "正常";
            } else if (status == "2") {
                return "无效";
            } else {
                return "";
            }
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
